<template>
  <div>
    <el-card>
      <div style="padding: 0 0 20px 0;">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>设备管理</el-breadcrumb-item>
          <el-breadcrumb-item>设备故障</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-form ref="form" :model="query" label-width="80px" :inline="true">
        <el-form-item>
          <el-date-picker
            v-model="query.startdate"
            placeholder="开始日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="query.enddate"
            placeholder="截止日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select v-model="query.errortype" clearable placeholder="错误类型">
            <el-option
              v-for="(item, index) in errortypeArr"
              :key="index"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="query.kw"
            clearable
            placeholder="设备名称、设备ID、分路名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch()">搜索</el-button>
        </el-form-item>
      </el-form>

      <div class="errors">
        <el-badge
          class="item"
          v-for="(item, index) in listNumArr"
          :key="index"
          :value="item.num"
        >
          <el-button @click="switchErrType(item.error)">{{
            errortypeArr[item.error]
          }}</el-button>
        </el-badge>
      </div>

      <el-table :data="dataList" border>
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="设备名称" prop="title"></el-table-column>
        <el-table-column label="设备ID" prop="deviceid"></el-table-column>
        <el-table-column label="设备地点" prop="address"></el-table-column>
        <el-table-column label="分路名称" prop="ln"></el-table-column>
        <el-table-column label="事件" prop="error">
          <template slot-scope="scope">
            {{ errortypeArr[scope.row.error] }}[
            {{
              unitArr[scope.row.error] == 'A'
                ? scope.row.value / 1000
                : scope.row.value
            }}
            {{ unitArr[scope.row.error] }}]
          </template>
        </el-table-column>
        <el-table-column label="故障时间" prop="updatetime">
          <template slot-scope="scope">
            {{ moment(scope.row.updatetime) }}
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :total="total"
        :current-page="query.page"
        :page-size="15"
        @current-change="changePage"
      ></el-pagination>
      <div style="height: 20px;"></div>
    </el-card>

    <el-dialog :visible.sync="showAddSubProject" title="新增子项目">
    </el-dialog>
  </div>
</template>

<script>
import { page } from '@/api/equipment/fault.js';
import moment from 'moment';
export default {
  data() {
    return {
      form: {},
      showAddSubProject: false,
      query: {
        page: 1,
        kw: '',
        startdate: moment().subtract(1, 'months').format('YYYY-MM-DD'),
        enddate: moment().format('YYYY-MM-DD'),
        errortype: '',
      },
      total: 0,
      dataList: [],
      errortypeArr: {},
      unitArr: {},
      listNumArr:{}
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      page(this.query).then((res) => {
        console.log(res, 'devices');
        this.dataList = res.data.list.data;
        this.total = res.data.list.total;
        this.errortypeArr = res.data.errortypeArr;
        this.unitArr = res.data.unitArr;
        this.listNumArr = res.data.listNumArr;
      });
    },
    changePage(e) {
      this.query.page = e;
      this.getData();
    },
    addSubProject() {
      this.showAddSubProject = true;
    },
    add() {},
    onSearch() {
      this.query.page = 1;
      this.getData();
    },
    moment(time) {
      return moment(time * 1000).format('YYYY-MM-DD HH:mm:ss');
    },
    switchErrType(errortype) {
      this.query.page = 1;
      this.query.errortype = errortype;
      this.getData();
    },
  },
};
</script>

<style scoped lang="scss">
.errors {
  display: flex;
  margin-bottom: 20px;
  flex-wrap: wrap;
  .item {
    margin-right: 40px;
    margin-bottom: 15px;
  }
}
</style>
